// components/searchDetail/searchDetail.js
const http = require("../../utils/http")
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    search_data_hd: ["综合排序", "全部频道", "筛选"],
    searchHdIndex: -1, //高亮索引
    filterItem: [0, 0, 0], //筛选 
    search_data_list: {
      "list_1": ["综合排序", "销量最高", "人气最旺", "最新上架", "最新更新"],
      "list_2": ["全部频道", "男生", "女生", "出版"],
      "list_3": [{
        "title": "完结状态",
        "text": ["全部", "完结", "连载"]
      }, {
        "title": "书籍价格",
        "text": ["全部", "免费", "包月", "收费"]
      }, {
        "title": "书籍字数",
        "text": ["全部", "30万以下", "30-50万字", "50-100万字", "100万字以上"]
      }]
    },
    // search_choose:{
    //   "list_1":[0,1,2,3,4],//排序
    //   "list_2":[0,1,2,3],//频道
    //   "list_3":[{
    //     "title_1":[0,1,2],
    //     "title_2":[0,1,2,3],
    //     "title_3":[0,1,2,3,4]
    //   }]
    // }
    search_choose: {
      "list_1": 0, //排序
      "list_2": 0, //频道
      "list_3": [{
        "title_1": 0,
        "title_2": 0,
        "title_3": 0
      }]
    }
  },
  attached() {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 重复点击逻辑
    search_hd(event) {
      let tempEven = event.currentTarget.dataset.hdindex;
      if (this.data.searchHdIndex == tempEven) {
        this.setData({
          searchHdIndex: -1
        })
      } else {
        this.setData({
          searchHdIndex: event.currentTarget.dataset.hdindex
        })
      }
      console.log('当前导航索引--->', event.currentTarget.dataset.hdindex);
    },
    secondItem(event) {
      let secIndex = this.data.searchHdIndex || 0; //对应下标 X
      let secYindex = event.target.dataset.secondindex || 0; //对应下标 Y
      // let oldArray = this.data.search_data_hd;
      let addText = event.target.dataset.seconditem
      // console.log('对应下标', secIndex, '旧数组', oldArray, '更改的值', addText);
      // let newArray = oldArray.splice(secIndex, 1, addText)
      console.log(secIndex, secYindex);
      let tempArray = "search_data_hd[" + secIndex + "]"
      this.setData({
        [tempArray]: addText
      })
      if (this.data.searchHdIndex >= -1) {
        // let tempChoose = this.data.search_choose
        if (secIndex == 0) {
          // console.log(tempChoose.list_1);
          let list_Choose_1 = "search_choose.list_1"
          this.setData({
            [list_Choose_1]: secYindex
          })
          // console.log('赋值后1---->',this.data.search_choose);
        } else if (secIndex == 1) {
          let list_Choose_2 = "search_choose.list_2"
          this.setData({
            [list_Choose_2]: secYindex
          })
          // console.log('赋值后2---->',this.data.search_choose);
        }
        // 调用方法
        this.getChooseData()
        console.log(this.data.search_choose.list_3[0].title_2);
      }
    },
    // 筛选
    filtrateItem(event) {
      let filterXnum = event.target.dataset.filterx;
      let filterYnum = event.target.dataset.filtery;
      // console.log(filterXnum,filterYnum);
      // console.log(this.data.search_choose.list_3[0]);
      let aaa = "filterItem[" + filterXnum + "]"
      this.setData({
        [aaa]: filterYnum
      });
      // console.log(this.data.filterItem);
      if (filterXnum == 0) {
        let title_choose_1 = "search_choose.list_3[0].title_1";
        this.setData({
          [title_choose_1]: filterYnum
        })
      } else if (filterXnum == 1) {
        let title_choose_2 = "search_choose.list_3[0].title_2";
        this.setData({
          [title_choose_2]: filterYnum
        })
      } else if (filterXnum == 2) {
        let title_choose_3 = "search_choose.list_3[0].title_3";
        this.setData({
          [title_choose_3]: filterYnum
        })
      }
      // 调用方法
      this.getChooseData()
      //  console.log('赋值后2---->',this.data.search_choose.list_3[0]);
    },
    // 遮罩层
    maskHidden() {
      this.setData({
        searchHdIndex: -1
      })
    },
    // 选择参数 
    getChooseData() {
      http.get("/search/book.json", {
        keyword: app.globalData.searchNovel,
        page: 1, //分页
        sort: this.data.search_choose.list_1, //排序
        channel: this.data.search_choose.list_2, //频道
        bookStatus: this.data.search_choose.list_3[0].title_1, //完结状态
        pay: this.data.search_choose.list_3[0].title_2, //价格
        wordCount: this.data.search_choose.list_3[0].title_3, //字数
      }).then(res => {
        console.log('查询成功---->', res);
        app.globalData.searchArra=[]
        app.globalData.searchArray = res
      })
    }
  }
})